package com.gxy.learn.backup.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ConfigurationProperties 方式
 */
@Data
@Component
@ConfigurationProperties(prefix = "autotable.common")
public class AutoTableConfig {
    @Autowired
    private Environment environment;
    private String assessStartHeaderStr;
    private String assessEndHeaderStr;
    private String assessPdfStartHeaderStr;
    private Map<String,String> assessPdfEndMapStr;
    private String assessPreStartHeaderStr;
    private String assessPreEndHeaderStr;
    private String adjustStartHeaderStr;
    private String adjustEndHeaderStr;
    private String promotionStartHeaderStr;
    private String promotionEndHeaderStr;
    /** 考核模块动态表头公共部分 */
    private Map<String,String> assessCommmonMapStr;
    /** 晋升申请动态表头公共部分 */
    private Map<String,String > promotionCommmonMapStr;
    private List<CommonTableHeadr> assessStartHeader;
    private List<CommonTableHeadr> assessEndHeader;
    private List<CommonTableHeadr> assessPdfStartHeader;
    private Map<String,List<CommonTableHeadr>> assessPdfEndMap = new HashMap<>();
    private List<CommonTableHeadr> assessPreStartHeader;
    private List<CommonTableHeadr> assessPreEndHeader;
    private List<CommonTableHeadr> adjustStartHeader;
    private List<CommonTableHeadr> adjustEndHeader;
    private List<CommonTableHeadr> promotionStartHeader;
    private List<CommonTableHeadr> promotionEndHeader;
    /** 考核模块动态表头公共部分 */
    private Map<String,List<CommonTableHeadr>> assessCommmonMap = new HashMap<>();
    /** 晋升申请动态表头公共部分 */
    private Map<String,List<CommonTableHeadr>> promotionCommmonMap = new HashMap<>();
    @EventListener(classes = {ApplicationEvent.class})
    public void envListener() {
        init();
    }
    @PostConstruct
    public void init() {
        this.assessStartHeader = JSONArray.parseArray(assessEndHeaderStr,CommonTableHeadr.class);
        this.assessEndHeader = JSONArray.parseArray(assessEndHeaderStr,CommonTableHeadr.class);
        this.assessPdfStartHeader = JSONArray.parseArray(assessPdfStartHeaderStr,CommonTableHeadr.class);
        for(Map.Entry<String,String> entry:assessPdfEndMapStr.entrySet()){
            this.assessPdfEndMap.put(entry.getKey(),JSONObject.parseArray(entry.getValue(),CommonTableHeadr.class));
        }
        this.assessPreStartHeader = JSONArray.parseArray(assessPreStartHeaderStr,CommonTableHeadr.class);
        this.assessPreEndHeader = JSONArray.parseArray(assessPreEndHeaderStr,CommonTableHeadr.class);
        this.adjustStartHeader = JSONArray.parseArray(adjustStartHeaderStr,CommonTableHeadr.class);
        this.adjustEndHeader = JSONArray.parseArray(adjustEndHeaderStr,CommonTableHeadr.class);
        this.promotionStartHeader = JSONArray.parseArray(promotionStartHeaderStr,CommonTableHeadr.class);
        this.promotionEndHeader = JSONArray.parseArray(promotionEndHeaderStr,CommonTableHeadr.class);
        for(Map.Entry<String,String> entry:assessCommmonMapStr.entrySet()){
            this.assessCommmonMap.put(entry.getKey(),JSONObject.parseArray(entry.getValue(),CommonTableHeadr.class));
        }
        for(Map.Entry<String,String> entry:promotionCommmonMapStr.entrySet()){
            this.promotionCommmonMap.put(entry.getKey(),JSONObject.parseArray(entry.getValue(),CommonTableHeadr.class));
        }


//        Integer sex = environment.getProperty("com.gxy.learn.am.user.sex",Integer.TYPE);
//        Integer age = environment.getProperty("com.gxy.learn.am.user.age",Integer.TYPE);
    }

}
